"""
# 对数变换实战_Yelp点评数量预测商家的平均评分
"""
# 使用对数变换后的Yelp点评数量预测商家的平均评分
import pandas as pd
import numpy as np
import json
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score

biz_file = open('../数据集/yelp_academic_dataset_business.json')
biz_df = pd.DataFrame([json.loads(x) for x in biz_file.readlines()])
# 注意，我们为原始点评数量加1，以免当点评数量为0时，对数运算结果得到负无穷大
biz_df['log_review_count'] = np.log10(biz_df['review_count'] + 1)

# 使用经过对数变换和未经对数变换的review_count特征，训练线性回归模型预测
# 一个商家的平均星级评分。比较两种模型的10-折交叉验证得分
m_orig = LinearRegression()
scores_orig = cross_val_score(m_orig, biz_df[['review_count']], biz_df['stars'], cv=10)
m_log = LinearRegression()
scores_log = cross_val_score(m_log, biz_df[['log_review_count']], biz_df['stars'], cv=10)
print("R-squared score without log transform: {:.5} (+/- {:.5})\n"
      "R-squared score with log transform: {:.5} (+/- {:.5})".format(scores_orig.mean(), scores_orig.std(),
                                                                     scores_log.mean(), scores_log.std()))
